// pages/coinCertificateShipping/coinCertificateShipping.js
import { shareOptions } from "../../utils/access";
import api from "../../config/api";
import request from "../../utils/request";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '', //订单id
    ids: [],// 订单id列表
    isBatchShipment: false,// 是否为批量发货
    detail: {}, //订单详情
    number: '',//单号
    companyList: [], //快递公司列表
    selectCompany: '', //选择的快递公司下标
    orderNum: 0,// 批量发货合并订单数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options?.id,
      ids: options?.ids,
      isBatchShipment: options?.isBatchShipment,
      orderNum: options?.orderNum
    })
    this.getDetail()
    this.getCompanyList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 获取订单详情
   */
  async getDetail(){
    let { id, isBatchShipment } = this.data
    let res = null
    if( isBatchShipment ){
      // 批量订单
      res = await request.get(api.earliestOrderDetail, {
        ids: id
      }, { loading: false })
    }else{
      // 单个订单
      res = await request.get(api.supervisorOrderDetail, {
        id
      }, { loading: false })
    } 
    if(res.httpStatus === 200){
      this.setData({
        detail: res.data
      })
      // 修改物流
      if(res.data.status === 2){
        this.setData({
          number: res.data.expressNo || '',
          selectCompany: res.data.expressName || ''
        })
      }
    }
  },
  /**
   * 获取快递公司列表
   */
  async getCompanyList(){
    let res = await request.get(api.expressCompanyList, {}, { loading: false })
    if(res.httpStatus === 200){
      this.setData({
        companyList: res.data
      })
    }
  },
  // 扫码
  scanCode(){
    wx.scanCode({
      success: (res) => {
        console.log('扫描结果:',res)
        if(res.result){
          this.setData({
            number: res.result
          })
        }else{
          wx.showToast({
            title: '未识别到可用快递单号',
            icon: 'none'
          })
        }
      },
      fail: (err)=>{
        console.log('扫描失败:',err)
        wx.showToast({
          title: '未识别到可用快递单号',
          icon: 'none'
        })
      }
    })
  },
  // 输入快递单号
  expressNumberChange(e){
    this.setData({
      number: e.detail.value
    })
  },
  // 选择快递公司
  companyChange(e){
     this.setData({
      selectCompany: e.currentTarget.dataset.name
     })
  },
  /**
   * 提交
   */
  async submit(){
    if(this.loading) return
    this.loading = true
    let { selectCompany: expressName, number: expressNo, id, detail: {status: type } } = this.data
    if(!expressNo || !expressName){
      this.loading = false
      return
    }else if(expressNo.match(/^[ ]*$/)){
      wx.showToast({
        title: '快递单号不能为空',
        icon: 'none'
      })
      this.loading = false
      return
    }
    let params = {
      id,
      expressName,
      expressNo,
      type
    }
    let res = await request.post(api.expressOrderSave, params, {
      loading: true
    })
    if(res.httpStatus === 200){
      wx.navigateBack()
      this.loading = false
    }else{
      setTimeout(()=>{
        wx.navigateBack()
        this.loading = false
      },1000)
    }
  },
  /**
   * 提交并继续
   */
  async submitNext(){
    if(this.loading) return
    this.loading = true
    let { selectCompany: expressName, number: expressNo, id} = this.data
    if(!expressNo || !expressName){
      this.loading = false
      return
    }else if(expressNo.match(/^[ ]*$/)){
      wx.showToast({
        title: '快递单号不能为空',
        icon: 'none'
      })
      this.loading = false
      return
    }
    let params = {
      id,
      expressName,
      expressNo,
      type: 1
    }
    let res = await request.post(api.expressOrderSaveNext, params, {
      loading: true
    })
    if(res.httpStatus === 200){
      this.setData({
        id: res.data.id,
        detail: res.data,
        number: '',
        selectCompany: ''
      })
      this.loading = false
    }else{
      setTimeout(()=>{
        wx.navigateBack()
        this.loading = false
      },1000)
    }
  },
  // 批量发货--提交
  async batchSubmit(){
    if(this.loading) return
    this.loading = true
    let { selectCompany: expressName, number: expressNo, id } = this.data
    if(!expressNo || !expressName){
      this.loading = false
      return
    }else if(expressNo.match(/^[ ]*$/)){
      wx.showToast({
        title: '快递单号不能为空',
        icon: 'none'
      })
      this.loading = false
      return
    }

    // 校验订单信息是否变更
    let res = await request.get(api.batchVerification, {
      ids: id,
    }, { loading: false })
    if(res.httpStatus === 200 ){
      if(res.data){
        this.sureSumit()
      }else{
        wx.showToast({
          title: '有订单发货状态已更改',
          icon: 'none'
        })
        this.loading = false
      }
    }else{
      this.loading = false
    }
  },
  // 通过验证
  async sureSumit(){
    let { selectCompany: expressName, number: expressNo, id } = this.data
    let params = {
      ids: id,
      expressName,
      expressNo
    }
    let res = await request.post(api.batchShipment, params, {
      loading: true
    })
    if(res.httpStatus === 200){
      wx.navigateBack()
      this.loading = false
    }else{
      this.loading = false
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    return shareOptions('default', e)
  }
})